/*
* FaceCat图形通讯框架(非开源)
* 著作权编号:2015SR229355+2020SR0266727
* 上海卷卷猫信息技术有限公司
*/

package facecat.topin.mine;

import facecat.topin.sock.*;
import java.util.*;

public class MyJava {

    public static void main(String[] args) throws ClassNotFoundException {
        try{
            //创建客户端监听
            ClientSocketListener clientListener = new ClientSocketListener();
            //注册监听
            FCClientSockets.setListener(clientListener);
            //创建服务端监听
            ServerSocketListener serverListener = new ServerSocketListener();
            //注册监听
            FCServerSockets.setListener(serverListener);
            //启动服务端socket
            int serverSocketID = FCServerSockets.start(1888);
            ArrayList<Integer> list = new ArrayList<Integer>();
            Random rd = new Random();
            for(int i = 0; i < 100; i++){
                //连接到服务端
                int socketID = FCClientSockets.connectTCP("127.0.0.1", 1888);
                list.add(socketID);
            }

            while(true){
                for(int l = 0; l < list.size(); l++){
                    int socketID = list.get(l);
                    //客户端发给服务端
                    if(socketID !=-1){
                        FCBinary bw = new FCBinary();
                        bw.writeInt(108);
                        bw.writeInt(100);
                        byte[] bytes = new byte[100];
                        for(int d = 0; d < bytes.length; d++){
                            bytes[d] = (byte)rd.nextInt(127);
                        }
                        bw.writeBytes(bytes);
                        byte[] sendBytes = bw.getBytes();
                        FCClientSockets.send(socketID, sendBytes, sendBytes.length);
                        bw.close();
                    }
                }
                Thread.sleep(1);
            }
        }catch(Exception ex){
            
        }
    }
}
